var editor; // use a global for the submit and return data rendering
var repeat;
$('#addButton').on('click', function () {
    var ids = [];
    var leaveAffairs = [];
    var leaveSicks = [];
    var leaveYears = [];
    var leaveMaternitys = [];
    var leaveMarriages = [];
    var leaveFunerals = [];
    $("input[name='id']").each(function (index, item) {

        if ($(this).val()) {
            ids[index] = $(this).val();
        } else {
            ids[index] = 0;
        }
    });
    $("input[name='sj']").each(function (index, item) {
        if ($(this).val()) {
            leaveAffairs[index] = $(this).val();
        } else {
            leaveAffairs[index] = 0;
        }

    });
    $("input[name='bj']").each(function (index, item) {
        if ($(this).val()) {
            leaveSicks[index] = $(this).val();
        } else {
            leaveSicks[index] = 0;
        }
    });
    $("input[name='nj']").each(function (index, item) {
        if ($(this).val()) {
            leaveYears[index] = $(this).val();
        } else {
            leaveYears[index] = 0;
        }
    });
    $("input[name='cj']").each(function (index, item) {
        if ($(this).val()) {
            leaveMaternitys[index] = $(this).val();
        } else {
            leaveMaternitys[index] = 0;
        }
    });
    $("input[name='hj']").each(function (index, item) {
        if ($(this).val()) {
            leaveMarriages[index] = $(this).val();
        } else {
            leaveMarriages[index] = 0;
        }
    });
    $("input[name='sangjia']").each(function (index, item) {
        if ($(this).val()) {
            leaveFunerals[index] = $(this).val();
        } else {
            leaveFunerals[index] = 0;
        }
    });
    var url = "/commis/api/attendance/create";
    if (repeat == 1) {
        url = url + "?repeat=1";
    }

    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: {
            ids: ids,
            leaveAffairs: leaveAffairs,
            leaveSicks: leaveSicks,
            leaveYears: leaveYears,
            leaveMaternitys: leaveMaternitys,
            leaveMarriages: leaveMarriages,
            leaveFunerals: leaveFunerals,

        },
        cache: false,

        success: function (result) {
            var status = result.status;
            var data = result.data;
            if (status == 0) {
                alert("添加成功,请记得上传对应详细文件");
                $.ajax({
                    url: '/commis/api/index/notify',
                    type: "GET",
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        if (result.status == 0) {
                            countNotify(result);
                        } else {
                            alert(result.msg);
                        }


                    }, error: function (msg) {
                        alert("发生错误");
                    }
                })
                // window.close();
            } else if (status == 1) {
                alert("添加失败，请检查");
            } else if (status == 4) {
                alert("已经存在当月数据,请不要重复提交！，若想要覆盖数据，请联系社区管理员");
                /* $.ajax({
                     type: "POST",
                     url: url,
                     dataType: "json",
                     data: {
                         ids: ids,
                         leaveAffairs: leaveAffairs,
                         leaveSicks: leaveSicks,
                         leaveYears: leaveYears,
                         leaveMaternitys: leaveMaternitys,
                         leaveMarriages: leaveMarriages,
                         leaveFunerals: leaveFunerals,

                     },
                     cache: false,

                     success: function (result) {
                         var status = result.status;
                         var data = result.data;
                         if (status == 0) {
                             alert("添加成功,请记得上传对应详细文件");
                             $.ajax({
                                 url: '/commis/api/index/notify',
                                 type: "GET",
                                 async: false,
                                 dataType: 'json',
                                 success: function (result) {
                                     if (result.status == 0) {
                                         countNotify(result);
                                     }else{
                                         alert(result.msg);
                                     }


                                 }, error: function (msg) {
                                     alert("发生错误");
                                 }
                             })
                             // window.close();
                         } else if (status == 1) {
                             alert("添加失败，请检查");
                         }  else if (status == 3) {
                             alert("请重新登录");
                             document.location.href = "../../login.html";
                         }

                     }, error: function () {
                         alert("发生错误,请检查所填写项是否都为整型数字，如果没有请填写0");

                     }
                 });*/
            } else if (status == 3) {
                alert("请重新登录");
                document.location.href = "../../login.html";
            }

        }, error: function () {
            alert("发生错误,请检查所填写项是否都为整型数字，如果没有请填写0");

        }
    });


});
$(document).ready(function () {
    var oDate = new Date(); //实例一个时间对象；
    var year = oDate.getFullYear();   //获取系统的年；
    var month = oDate.getMonth() + 1;   //获取系统月份，由于月份是从0开始计算，所以要加1
    repeat = getUrlParam("repeat");
    if (repeat != 1) {
        repeat = 0;
    }


    $("#inputfile").fileinput({
        uploadUrl: "/commis/api/upload/uploadAttFile?repeat=" + repeat, // server upload action
        uploadAsync: true,
        showPreview: false,
        language: 'zh',
        uploadExtraData: {id: '${submission.missionId}'},
        maxFileCount: 1,
        elErrorContainer: ".file-error",
        allowedFileExtensions: ["xls", "xlsx"]
    }).on("fileuploaded", function (event, data, id, index) {
        var response = data.response;
        var status = response.status;
        var out;
        if (status == 0) {
            var fname = data.files[index].name;
            out = '上传文件  - ' +
                fname + ' 成功.';
            alert("上传成功");
        }

        else if (status == 1) {
            alert("上传失败");
        } else {
            out = '当月文件已经上传，请不要重复上传';
            alert("当月文件已经上传，请不要重复上传");

        }
        $('.file-success').html(out);
        $('.file-success').fadeIn('slow');
        // $('#inputfile').parent().prev().hide();


    }).on('fileclear', function (event) {
        $('#inputfile').val("");
        // $('#inputfile').parent().prev().show();
        $('.file-success').hide();

    });


    $.ajax({
        url: '/commis/api/person/communityPerson',
        async: false,
        dataType: 'json',
        success: function (response) {
            var data = response.data;
            for (var i = 0; i < data.length; i++) {
                $("#mytbody").append("<tr><td>" + data[i].personName + "<input name='id' value='" + data[i].personId + "' type='hidden'/></td>"
                    + "<td><input name='sj' value='' style='width: 40px'/></td>"
                    + "<td><input name='bj' value='' style='width: 40px'/></td>"
                    + "<td><input name='nj' value='' style='width: 40px'/></td>"
                    + "<td><input name='cj' value='' style='width: 40px'/></td>"
                    + " <td><input name='hj' value='' style='width: 40px'/></td>"
                    + "  <td><input name='sangjia' value='' style='width: 40px'/></td>"
                    + "  <td>" + year + "</td>"
                    + "  <td>" + month + "</td>"
                    + " </tr>");
            }


        }, error: function () {
            alert("发生错误");

        }
    });


})
//设置框架
$(function () {
    $("#page_style").frame({
        float: 'left',
        menu_nav: '.Quick_operation',
        color_btn: '.skin_select',
        Sellerber_menu: '.list_content',
        Sellerber_header: '.Sellerber_header',
    });
});
$("#iframe_box").niceScroll({
    cursorcolor: "#888888",
    cursoropacitymax: 1,
    touchbehavior: false,
    cursorwidth: "5px",
    cursorborder: "0",
    cursorborderradius: "5px"
});
/*********************/
require.config({
    paths: {
        echarts: './js/dist'
    }
});